package com.ems.sys.controller;

import com.ems.entity.Result;
import com.ems.entity.StatusCode;
import com.ems.sys.pojo.UserInfo;
import com.ems.sys.pojo.Users;
import com.ems.sys.pojo.Users_Roles;
import com.ems.sys.service.UserService;
//import com.ems.sys.service.impl.UserDataServiceImpl;
import com.ems.sys.service.Users_RolesService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:传智播客
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/sys/users")

//@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private Users_RolesService users_rolesService;

    /***
     * Emlpoyee分页条件搜索实现
     * @param users
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Users users, @PathVariable  int page, @PathVariable  int size){

        //调用userService实现分页条件查询Emlpoyee
        PageInfo<Users> pageInfo = userService.findPage(users, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Emlpoyee分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用userService实现分页查询Emlpoyee
        PageInfo<Users> pageInfo = userService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param users
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Users>> findList(@RequestBody(required = false) Users users){
        //调用userService实现条件查询Emlpoyee
        List<Users> list = userService.findList(users);
        return new Result<List<Users>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用userService实现根据主键删除
        userService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    @DeleteMapping(value = "/role/{roleId}" )
    public Result deleteRoleByRoleId(@PathVariable String roleId){
        users_rolesService.deleteRole(roleId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Emlpoyee数据
     * @param users
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Users users, @PathVariable String id){
        //设置主键值
        users.setuId(id);
        //调用userService实现修改Emlpoyee
        userService.update(users);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Emlpoyee数据
     * @param users
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Users users){
        //调用userService实现添加Emlpoyee
        userService.add(users);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    @PostMapping("/role")
    public Result addRole(@RequestBody List<Users_Roles> users_roles){
        //调用userService实现添加Emlpoyee
        users_rolesService.addRole(users_roles);
        return new Result(true,StatusCode.OK,"添加成功");
    }
    /***
     * 根据ID查询Emlpoyee数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Users> findById(@PathVariable String id){
        //调用userService实现根据主键查询Emlpoyee
        Users users = userService.findById(id);
        return new Result<Users>(true,StatusCode.OK,"查询成功",users);
    }

//    @GetMapping("/{id}")
//    public Result<Users> findByUsername(@PathVariable String id){
//        //调用userService实现根据主键查询Emlpoyee
//        Users users = userService.findById(id);
//        return new Result<Users>(true,StatusCode.OK,"查询成功",users);
//    }


    /***
     * 查询Emlpoyee全部数据
     * @return
     */
    @GetMapping
    public Result<List<Users>> findAll(){
        //调用userService实现查询所有Emlpoyee
        List<Users> list = userService.findByUsername();
        return new Result<List<Users>>(true, StatusCode.OK,"查询成功",list) ;
    }


}
